package cn.aimee.webviewjsdemo;

import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.btn_call_js)
    Button btnCallJs;

    private WebViewUtil webViewUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        webViewUtil = new WebViewUtil();
        initWebView();
    }

    @OnClick({R.id.btn_call_js})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_call_js:
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    webview.loadUrl("javascript:callJS()");
                } else {
                    webview.evaluateJavascript("javascript:callJS()", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            Log.i("zhihua", "收到的结果：" + value);
                        }
                    });
                }
                break;
        }
    }

    private void initWebView() {
        WebSettings webSettings = webview.getSettings();

        webSettings.setJavaScriptEnabled(true);//设置与JS交互的权限
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//设置允许JS弹窗

        //通过addJavascriptInterface()将Java对象映射到JS对象
        //参数1：Javascript对象名
        //参数2：Java对象名
        webview.addJavascriptInterface(new AndroidtoJs(), "test");

        webview.loadUrl("file:///android_asset/test.html");//先载入JS代码
        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Alert")
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                }).setCancelable(false)
                .create().show();
                return true;
            }
        });
    }

    @Override
    protected void onDestroy() {
        webViewUtil.destroy(webview);
        super.onDestroy();
    }
}